package com.zhika.zishi.fragments.login;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sina.weibo.sdk.auth.WeiboAuth;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.zhika.zishi.R;
import com.zhika.zishi.constant.WeiboConstant;
import com.zhika.zishi.customview.OtherWayView;
import com.zhika.zishi.empower.SinaAuthListener;
import com.zhika.zishi.interfaces.OtherWayInterface;

/**
 * Created by Michael on 2015/4/3.
 */
public class LoginFragment extends Fragment implements View.OnClickListener, OtherWayInterface {

    private static final String TAG = "LoginActivity";

    private Button registerButton;

    private OtherWayView loginOtherWayView;

    //微博授权时调用
    private SsoHandler ssoHandler;

    private Button loginButton;

    //该界面的view
    private View view;

    //上下文
    private Context context;

    //单例模式下的fragment
    private static LoginFragment loginFragment;

    //忘记密码的按钮
    private Button forgetButton;

    //账号
    private EditText userEdit;

    //密码
    private EditText passwordEdit;

    /**
     * 在activity中实现此接口的方法，控制该fragment的事件
     */
    public interface OnLoginListener {

        //登录事件
        public void login(String user, String password);

        //注册事件
        public void loginRegisterListener();

        //忘记密码的事件
        public void loginForgetPassword();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.login_layout, container, false);

        initView();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        resetWidget();
    }

    /**
     * 重置登录界面控件的值
     */
    private void resetWidget(){
        userEdit.setText(null);
        passwordEdit.setText(null);
    }
    /**
     * 初始化界面上的控件
     */
    private void initView() {

        context = getActivity();

        registerButton = (Button) view.findViewById(R.id.login_register_button);
        registerButton.setOnClickListener(this);

        loginOtherWayView = (OtherWayView) view.findViewById(R.id.login_other_way);
        loginOtherWayView.setOtherWayInterface(this);

        loginButton = (Button) view.findViewById(R.id.login_button);
        loginButton.setOnClickListener(this);

        forgetButton = (Button) view.findViewById(R.id.login_forget_secret_button);
        forgetButton.setOnClickListener(this);

        userEdit = (EditText) view.findViewById(R.id.login_user_edit);

        passwordEdit = (EditText) view.findViewById(R.id.login_password_edit);
        passwordEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               String user = userEdit.getText().toString();
                if (user.length() == 11 && s.length()>0){
                    loginButton.setBackground(getResources().getDrawable(R.drawable.yellow_edging_shape));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (context instanceof OnLoginListener) {
            switch (v.getId()) {
                case R.id.login_register_button:
                    ((OnLoginListener) context).loginRegisterListener();
                    break;
                case R.id.login_button:
                    loginButtonListener();
                    break;
                case R.id.login_forget_secret_button:
                    ((OnLoginListener) context).loginForgetPassword();
                    break;
            }
        }
    }

    @Override
    public void weixinButtonListener() {
        Toast.makeText(context, "微信登录", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void sinaButtonListener() {
        WeiboAuth weiboAuth = new WeiboAuth(context, WeiboConstant.WEIBO_APP_KEY, WeiboConstant.WEIBO_CALL_BACK_URL, WeiboConstant.WEIBO_SCOPE);
        ssoHandler = new SsoHandler((Activity) context, weiboAuth);
        ssoHandler.authorize(new SinaAuthListener(context));
//        Toast.makeText(LoginActivity.this,"微博登录",Toast.LENGTH_SHORT).show();

    }

    /**
     * 登录事件
     */
    private void loginButtonListener() {
        String user = userEdit.getText().toString();
        if (user == null || user.equals("")) {
            showToast("请输入账号");
        }else if (user.length()!=11){
            showToast("账号格式不对");
        }else {
            String password = passwordEdit.getText().toString();
            if (password == null || password.equals("")){
                showToast("请输入密码");
            }else {
                ((OnLoginListener) context).login(user, password);
            }
        }
    }


    /**
     * 以下注释现在不管用了
     */
//    /**
//     * 微博验证时，如果不重新写OnActivityResult方法，就会先到登录界面，在到自己想要去的界面
//     * @param requestCode
//     * @param resultCode
//     * @param data
//     */
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//
//        if (ssoHandler != null){
//            ssoHandler.authorizeCallBack(requestCode,resultCode,data);
//        }
//        super.onActivityResult(requestCode, resultCode, data);
//    }

    /**
     * 显示吐丝
     *
     * @param msg
     */
    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}
